package com.iwuke.dance.gadget_view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.iwuke.dance.R;

import androidx.core.content.res.ResourcesCompat;

/**
 * @author: 魏从响
 * @date: 2025/9/12
 */
public class TitleBarView extends RelativeLayout implements View.OnClickListener {

    private TextView tv_title_bar;
    private TextView tv_connect;
    private RelativeLayout rlt_title;
    private ImageView img_left;
    private OnClickListener leftClick,onRightListener,onRightImageViewListener;
    private TextView tv_right;
    private ImageView img_add;
    private TextView tv_title_bar_left;
    private RelativeLayout rlt_left;
    private OnClickListener onParkingListener;
    private TextView tv_right_box;

    public TitleBarView(Context context) {
        super(context);

    }

    public TitleBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        View inflate = LayoutInflater.from(context).inflate(R.layout.title_bar_back, this);

        rlt_title = inflate.findViewById(R.id.rlt_title);
        tv_title_bar = inflate.findViewById(R.id.tv_title_bar);
        img_left = inflate.findViewById(R.id.img_left);
        tv_connect = inflate.findViewById(R.id.tv_connect);
//        tv_right = inflate.findViewById(R.id.tv_right);
        img_add = inflate.findViewById(R.id.img_add);
        rlt_left = inflate.findViewById(R.id.rlt_left);
        tv_title_bar_left = inflate.findViewById(R.id.tv_title_bar_left);
        tv_right = inflate.findViewById(R.id.tv_right);
        tv_right_box = inflate.findViewById(R.id.tv_right_box);

        img_left.setOnClickListener(this);
//        tv_right.setOnClickListener(this);
        img_add.setOnClickListener(this);
        rlt_left.setOnClickListener(this);
        tv_right.setOnClickListener(this);
        tv_right_box.setOnClickListener(this);
    }

    public TitleBarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setTitleBarVisibility(boolean titleBarFlag){
        if (titleBarFlag){
            tv_title_bar.setVisibility(VISIBLE);
        }else {
            tv_title_bar.setVisibility(GONE);
        }
    }

    public void setLeftVisibility(boolean leftVisibility){
        if (leftVisibility){
            img_left.setVisibility(VISIBLE);
        }else {
            img_left.setVisibility(GONE);
        }
    }

    public void setConnectVisibility(boolean connectFlag){
        if (connectFlag){
            tv_connect.setVisibility(VISIBLE);
        }else {
            tv_connect.setVisibility(GONE);
        }
    }

    public void setTitleBackgroundColor2(int bgColor){
//        rlt_title.setBackground(getResources().getDrawable(bgColor,null));
        // 修改后
        rlt_title.setBackground(ResourcesCompat.getDrawable(getResources(), bgColor, null));
    }

    public void setTitleBackgroundColor(int bgColor){
        rlt_title.setBackgroundColor(getResources().getColor(bgColor,null));
    }

    public void setTitleColor(int titleColor){
        tv_title_bar.setTextColor(getResources().getColor(titleColor,null));
    }

    public void setTitleText(String titleText){
        tv_title_bar.setText(titleText);
    }

    public void setLeftColor(int color){
        img_left.setColorFilter(getResources().getColor(color,null));
    }
    public void setLeftOnClick(OnClickListener leftOnClick){
        leftClick = leftOnClick;
    }

    public void setRightVisibility(boolean rightVisibility){
        if (rightVisibility){
            tv_right.setVisibility(VISIBLE);
        }else {
            tv_right.setVisibility(GONE);
        }
    }

    public void setRightText(String rightText){
        tv_right.setText(rightText);
    }
    public void setRightColor(int color){
        tv_right.setTextColor(getResources().getColor(color,null));
    }
    public void setRightBackground(int bgColor){
        tv_right.setBackground(ResourcesCompat.getDrawable(getResources(),bgColor,null));
    }

    public void setOnRightListener(OnClickListener onRightClick){
        onRightListener=onRightClick;
    }

    public void setRightBoxVisibility(boolean rightVisibility){
        if (rightVisibility){
            tv_right_box.setVisibility(VISIBLE);
        }else {
            tv_right_box.setVisibility(GONE);
        }
    }

    public void setRightBoxText(String rightText){
        tv_right_box.setText(rightText);
    }
    public void setRightBoxColor(int color){
        tv_right_box.setTextColor(getResources().getColor(color,null));
    }
    public void setRightBoxBackground(int bgColor){
        tv_right_box.setBackground(ResourcesCompat.getDrawable(getResources(), bgColor,null));
    }

    public void setOnRightBoxListener(OnClickListener onRightClick){
        onRightListener=onRightClick;
    }

    public void setRightImageViewVisibility(boolean rightVisibility){
        if (rightVisibility){
            img_add.setVisibility(VISIBLE);
        }else {
            img_add.setVisibility(GONE);
        }
    }

    public void setRightImageViewColor(int bgColor){
        img_add.setImageResource(bgColor);
    }

    public void setRightImageViewTin(int bgColor){
        img_add.setColorFilter(getResources().getColor(bgColor,null));
    }

    public void setOnRightImageViewListener(OnClickListener onRightImageViewClick){
        onRightImageViewListener=onRightImageViewClick;
    }

    public void setOnParkingListener(OnClickListener onParkingListener){
        this.onParkingListener = onParkingListener;
    }

    public void setParkingVisibility(boolean rightVisibility){
        if (rightVisibility){
            rlt_left.setVisibility(VISIBLE);
        }else {
            rlt_left.setVisibility(GONE);
        }
    }

    public TextView setParking(){
        return tv_title_bar_left;
    }

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.img_left){
            if (leftClick!=null)
                leftClick.onClick(v);
        }else if (v.getId()==R.id.img_add){
            if (onRightImageViewListener!=null){
                onRightImageViewListener.onClick(v);
            }
        }if (v.getId()==R.id.rlt_left){
            if (onParkingListener!=null) {
                onParkingListener.onClick(v);
            }
        }else if (v.getId()==R.id.tv_right){
            if (onRightListener!=null)
                onRightListener.onClick(v);
        }else if (v.getId()==R.id.tv_right_box){
            if (onRightListener!=null)
                onRightListener.onClick(v);
        }

        // switch不再允许变量，只允许常量（编译报错）
//        switch (v.getId()){
//            case R.id.img_left:
//                if (leftClick!=null)
//                    leftClick.onClick(v);
//                break;
//
////            case R.id.tv_right:
////                if (onRightListener!=null)
////                    onRightListener.onClick(v);
////                break;
//
//            case R.id.img_add:
//                if (onRightImageViewListener!=null){
//                    onRightImageViewListener.onClick(v);
//                }
//                break;
//
//            case R.id.rlt_left:
//                if (onParkingListener!=null) {
//                    onParkingListener.onClick(v);
//                }
//                break;
//        }
    }
}
